package com.shujia.flink.core

import org.apache.flink.streaming.api.scala._

object Demo1WordCount {

  def main(args: Array[String]): Unit = {

    /**
      * 创建flink环境
      *
      */

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //并行的
    env.setParallelism(1)

    /**
      * 读取数据
      *
      * 读取socket 数据
      *
      * nc -lk 8888
      *
      */

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)


    /**
      * 数据处理
      *
      */

    val wordsDS: DataStream[String] = linesDS.flatMap(line => line.split(","))

    val kvDS: DataStream[(String, Int)] = wordsDS.map(word => (word, 1))

    //按照单词进行分组 （将同一个单词分到同一个流中）
    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(kv => kv._1)

    /**
      * flink  算子本身就是有状态的算子
      *
      */

    //对哪一个列求和
    val countDS: DataStream[(String, Int)] = keyByDS.sum(1)


    /**
      * 打印数据
      *
      */

    countDS.print()


    /**
      * 启动flink 程序
      *
      */

    env.execute()


  }

}
